//
// Created by lenovo on 2024/6/28.
//

#ifndef TEYVATSURVIVAL_UTILS_VECTOR_H
#define TEYVATSURVIVAL_UTILS_VECTOR_H
#include <array>
#include <iostream>
#include <cmath>

namespace Util{
// 通用的 Vector 类模板
template <std::size_t D, typename T>
    class Vector {
    public:
        // 默认构造函数
        Vector() {
            value.fill(T{});
        }

        // 初始化列表构造函数
        Vector(std::initializer_list<T> list) {
            std::size_t i = 0;
            for (auto& elem : list) {
                value[i++] = elem;
                if (i >= D) break;
            }
        }

        // 元素访问
        T& operator[](std::size_t index) {
            return value[index];
        }

        const T& operator[](std::size_t index) const {
            return value[index];
        }

        // 向量加法
        Vector<D, T> operator+(const Vector<D, T>& other) const {
            Vector<D, T> result;
            for (std::size_t i = 0; i < D; ++i) {
                result[i] = value[i] + other[i];
            }
            return result;
        }

        // 向量减法
        Vector<D, T> operator-(const Vector<D, T>& other) const {
            Vector<D, T> result;
            for (std::size_t i = 0; i < D; ++i) {
                result[i] = value[i] - other[i];
            }
            return result;
        }

        // 缩放乘法
        Vector<D, T> operator*(T scalar) const {
            Vector<D, T> result;
            for (std::size_t i = 0; i < D; ++i) {
                result[i] = value[i] * scalar;
            }
            return result;
        }

        // 缩放除法
        Vector<D, T> operator/(T scalar) const {
            Vector<D, T> result;
            for (std::size_t i = 0; i < D; ++i) {
                result[i] = value[i] / scalar;
            }
            return result;
        }

        // 比较运算符
        bool operator==(const Vector<D, T>& other) const {
            for (std::size_t i = 0; i < D; ++i) {
                if (value[i] != other[i]) return false;
            }
            return true;
        }

        bool operator!=(const Vector<D, T>& other) const {
            return !(*this == other);
        }

        // 计算向量的长度
        T magnitude() const {
            T sum = T{};
            for (std::size_t i = 0; i < D; ++i) {
                sum += value[i] * value[i];
            }
            return std::sqrt(sum);
        }

        static size_t dimension(){
            return D ;
        }
    public:
        std::array<T, D> value;
    };

template <typename T>
using Vector2 = Vector<2,T>;
using Vector2f = Vector<2,float>;
using Vector2i = Vector<2,int>;

}
#endif //TEYVATSURVIVAL_UTILS_VECTOR_H
